var getCurrentMinimumBidPinger;

function hideAddBidMessageContainer() {
	$("#addBidMessageContainer").hide()
}

function getShowListingID() {
	return $('#listing_id').val()
}

function getCurrentMinimumBidResponse() {
	if (getShowListingID() != null) {
		$.ajax({
			url : '/AuctionHausen/listing/getCurrentMinimumBid',
			data : {
				listing_id : getShowListingID()
			},
			success : function display(d, s, r) {
				updateMinimumBidPrice(d)
			}
		})
	} else {
		window.clearInterval(getCurrentMinimumBidPinger)
	}
}

function show_listing_init() {
	hideAddBidMessageContainer()
	getCurrentMinimumBidPinger = window.setInterval(
			getCurrentMinimumBidResponse, 10000);
}

function expireListing() {
	$('#show-listing')[0].innerHTML = '<h1 id="listing_expired_header">Listing Expired</h1>';
}

function addBidSuccess(response) {
	if (response.expired == true) {
		expireListing()
	} else {
		showBidResponseMessage(response.message)
		if (response.failed == false) {
			updateDisplayedMinimumBidPrice(response.nextBidAmount)
		}
	}
}

function showBidResponseMessage(bidResponseMessage) {
	hideAddBidMessageContainer()
	$('#addBidMessageContainer')[0].innerHTML = bidResponseMessage
	$("#addBidMessageContainer").show('slow')
	var t = setTimeout('hideAddBidMessageContainer()', 3000);
}

function updateMinimumBidPrice(newMinimumBidValueResponse) {
	if (newMinimumBidValueResponse.expired == true) {
		expireListing()
	} else {
		updateDisplayedMinimumBidPrice(newMinimumBidValueResponse.value)
	}
}

function updateDisplayedMinimumBidPrice(value) {
	if ($('#bidSelector')[0].value != value) {
		$('#bidSelector')[0].value = value
	}
}

function loadRecentBids(url) {

	alert(url);
	$.post(url,
		function(data) {
			alert(data['class']);
			$('#top-10-bids')[0].innerHTML = data['class'];
		}
	);
}

